<?php

class MatchTypeController extends \BaseController {

	/**
	 * Controller constructor
	 */
	public function __construct() {
		$this->beforeFilter('auth', array('except' => array('getAPI', 'storeAPI', 'updateAPI', 'destroyAPI')));
	}
	
	/**
	 * Display a listing of match types.
	 *
	 * @return Response
	 */
	public function index()
	{
		// get all the matches
		$matchTypes = MatchType::all();

		// load the view and pass the matches
		return View::make('matchTypes.index')
			->with('matchTypes', $matchTypes);
	}


	/**
	 * Show the form for creating a new match type.
	 *
	 * @return Response
	 */
	public function create()
	{
		return View::make('matchTypes.create');
	}


	/**
	 * Store a newly created match type in storage.
	 *
	 * @return Response
	 */
	public function store()
	{
		// validate
		$rules = array(
			'name'       => 'required',
			'totalPlayers'      => 'required|numeric'
		);
		$validator = Validator::make(Input::all(), $rules);

		// process the login
		if ($validator->fails()) {
			return Redirect::to('matchTypes/create')
				->withErrors($validator)
				->withInput(Input::except('password'));
		} else {
			// store
			$matchType = new MatchType();
			$matchType->name       = Input::get('name');
			$matchType->totalPlayers      = Input::get('totalPlayers');
			$matchType->versus      = Input::get('versus') === '1'? 1 : 0;
			$matchType->save();

			// redirect
			Session::flash('message', 'Tipo de partido creado!');
			return Redirect::to('matchTypes');
		}
	}


	/**
	 * Display the specified match type.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function show($id)
	{
		//
	}


	/**
	 * Show the form for editing the specified match type.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function edit($id)
	{
		// get the nerd
		$matchType = MatchType::find($id);

		// show the edit form and pass the nerd
		return View::make('matchTypes.edit')
			->with('matchType', $matchType);
	}


	/**
	 * Update the specified match type in storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function update($id)
	{
		// validate
		$rules = array(
			'name'       => 'required',
			'totalPlayers'      => 'required|numeric'
		);
		$validator = Validator::make(Input::all(), $rules);
		
		// process the login
		if ($validator->fails()) {
			return Redirect::to('matchTypes/' . $id . '/edit')
				->withErrors($validator)
				->withInput(Input::except('password'));
		} else {
			// store
			$matchType = MatchType::find($id);
			$matchType->name       = Input::get('name');
			$matchType->totalPlayers      = Input::get('totalPlayers');
			$matchType->versus      = Input::get('versus') === '1'? '1' : '0';
			$matchType->save();

			// redirect
			Session::flash('message', 'Tipo de partido actualizado!');
			return Redirect::to('matchTypes');
		}
	}


	/**
	 * Remove the specified match type from storage.
	 *
	 * @param  int  $id
	 * @return Response
	 */
	public function destroy($id)
	{
		// delete
		$matchType = MatchType::find($id);
		$matchType->delete();

		// redirect
		Session::flash('message', 'Tipo de partido eliminado!');
		return Redirect::to('matchTypes');
	}
	
	/*
	 * API Methods
	*/
	/**
	 * Display a listing of matchTypes in JSON format.
	 *
	 * @return JSON Response
	 */
	public function getAPI($id = null)
	{
		if (is_null($id))
			return Response::json(MatchType::all());
		else
		{
			$matchType = MatchType::find($id);
			if(is_null($matchType)){
				return Response::json('MatchType not found', 404);
			} else {
				return Response::json($matchType);
			}
		}
	}
	
	/**
	 * Store a newly created matchType in storage from JSON posted data.
	 *
	 * @return JSON Response
	 */
	public function storeAPI()
	{
		$newMatchType = Input::json();
	
		$matchType = new MatchType();
		$matchType->name = $newMatchType->get('name');
		$matchType->totalPlayers = $newMatchType->get('totalPlayers');
		$matchType->versus = $newMatchType->get('versus');
		$matchType->save();
	
		return Response::json($matchType);
	}
	
	/**
	 * Update the specified matchType in storage from JSON put data.
	 *
	 * @return JSON Response
	 */
	public function updateAPI()
	{
		$updateMatchType = Input::json();
	
		$matchType = MatchType::find($updateMatchType->get('id'));
		if(is_null($matchType)){
			return Response::json('MatchType not found', 404);
		}
		$matchType->name = $updateMatchType->get('name');
		$matchType->totalPlayers = $updateMatchType->get('totalPlayers');
		$matchType->versus = $updateMatchType->get('versus');
		$matchType->save();
	
		return Response::json($matchType);
	}
	
	/**
	 * Remove the specified matchType from storage by posted id.
	 *
	 * @return JSON Response
	 */
	public function destroyAPI($id)
	{
		$matchType = MatchType::find($id);
	
		if(is_null($matchType))
		{
			return Response::json('MatchType not found', 404);
		}
		$deletedMatchType = $matchType;
		$matchType->delete();
		return Response::json($deletedMatchType);
	}


}
